package edu.ufl.cise.sanitas.client;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import edu.ufl.cise.sanitas.client.proxy.Proxy;
import edu.ufl.cise.sanitas.types.comm.NewPostRequest;
import edu.ufl.cise.sanitas.types.comm.Response;
import edu.ufl.cise.sanitas.types.data.Condition;
import edu.ufl.cise.sanitas.types.data.Feed;

public class NewPostEdit extends Activity {

	private static final int MESSAGECODE_SEND = 0;
	private static final String LOG_TAG = "sanitas.client.NewPostEdit";
	
	private ProgressDialog pd;
	private Context mCtx;
	private Proxy mProxy;
	private Feed currentFeed;
	
	private Handler handler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			Response response = (Response) msg.obj;
			switch (msg.what) {
			case MESSAGECODE_SEND:
				switch (response.getStatus()) {
				case SUCCESS:
				case FAIL:
					pd.dismiss();
					Intent intent = new Intent();
	                setResult(RESULT_OK, intent);
	                finish();
					break;
				case TIMEOUT:
					pd.dismiss();
					mProxy.storePostLocally(currentFeed);
					Intent timeoutIntent = new Intent();
					setResult(RESULT_OK, timeoutIntent);
					finish();
					break;
				}
				break;
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newpost_edit);
		
		mCtx = this;
		mProxy = Proxy.getInstance();
		
		Spinner s = (Spinner) findViewById(R.id.newpost_spinner);
		ArrayAdapter<Condition> adapter = new ArrayAdapter<Condition>(this, android.R.layout.simple_spinner_item, Proxy.getConditionList());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		
		Button sendButton = (Button) findViewById(R.id.newpost_btn_confirm);
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Condition condition = Proxy.getConditionList()[((Spinner) findViewById(R.id.newpost_spinner)).getSelectedItemPosition()];
				String content = ((EditText) findViewById(R.id.newpost_edit)).getText().toString();
				NewPostRequest request = new NewPostRequest(new Timestamp(new Date().getTime()).toString(), mProxy.getConnectionStatus(), condition, content, Proxy.getUser().getUserId(), Proxy.getUser().getName());
				currentFeed = new Feed(new Timestamp(new Date().getTime()).toString(), Proxy.getUser().getName(), content, condition.getConditionName());
				pd = ProgressDialog.show(mCtx, "New Post", "Sending");
				mProxy.sendRequest(request, handler, MESSAGECODE_SEND);
			}
			
		});
		
		Button canceButton = (Button) findViewById(R.id.newpost_btn_cancel);
		canceButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent mIntent = new Intent();
				setResult(RESULT_OK, mIntent);
				finish();
			}
			
		});
		
	}

}
